home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / lib / xpmtopix.c < prev   
C/C++ Source or Header  |  1996-07-24  |  6KB  |  216 lines

  1. #include <includes.h>
  2. #include <config.h>
  3.  
  4. /* Usage: xpmtopix (outputfile).  The output files will have .1, .2, etc
  5.  * appended to the name to denote the part
  6.  * This program does assume that only 1 character per color is needed
  7.  * for the xpm files, and assumes that the final output file will work
  8.  * in the same space
  9.  */
  10.  
  11. /* Most colors that will be supported for output */
  12. #define MAX_COLORS    91
  13. #define START_COLOR    35    /* What ascii code to start new colors at */
  14.  
  15. /* image_number we are on for this output file */
  16. int image_number = 0,file_number=1, image_y=0, image_x=0;
  17.  
  18. /* This contains the color strings from the xpm files, to match
  19.  * up with.
  20.  */
  21. int num_color_match=0;
  22. char color_match[MAX_COLORS][80];
  23. char xpm_buf[XPM_MONTAGE_Y*24][XPM_MONTAGE_X*24 + 1];
  24.  
  25. void load_xpm(char *filename)
  26. {
  27.     FILE *fin;
  28.     int  colors=0, dummy, num_color_trans=0;
  29.     char buf[256], color_translate[MAX_COLORS][2];
  30.  
  31.     if ((fin=fopen(filename, "r"))==NULL) {
  32.     fprintf(stderr, "Could not open %s\n", filename);
  33.     exit(8);
  34.     }
  35.     while ((fgets(buf, 256, fin)!=NULL) && (strncmp(buf, "static", 6)));
  36.  
  37.     fgets(buf, 256,fin);
  38.     if (buf[0]=='/') {
  39.         fgets(buf, 256,fin);
  40.     }
  41.     sscanf(buf,"\"%d %d %d", &dummy, &dummy, &colors);
  42.  
  43.     /* Start reading in colors */
  44.     dummy = 0;
  45.     while (dummy<colors) {
  46.     int color_char_match = -1, count;
  47.  
  48.     if (getc(fin)=='/') {
  49.         fgets(buf, 256,fin);    /* read in rest of line */
  50.         if (dummy!=0 || strncmp(buf,"* colors",8))
  51.             fprintf(stderr,"File %s, Unexpected comment: %s\n", filename, buf);
  52.         continue;
  53.     }
  54.     fgets(buf, 256, fin);
  55.     buf[strlen(buf)-3] ='\0'; /* removing newline, comma, and quote */
  56.  
  57.     for (count=0; count<num_color_match; count++) {
  58.         if (!strcasecmp(color_match[count]+1, buf+1)) break;
  59.         if (color_match[count][0] == buf[0])
  60.         color_char_match = count;
  61.     }
  62.     if (count==num_color_match) {
  63.         /* If the character for that color is already used,
  64.          * then increase the last one in the array by one and
  65.          * use that.  Always sort after each new color is
  66.          * added.
  67.          */
  68.         if (color_char_match!=-1 || (buf[0]>(START_COLOR + num_color_match))) {
  69.         color_translate[num_color_trans][0] = buf[0];
  70.         buf[0] = START_COLOR + num_color_match;
  71.         color_translate[num_color_trans][1] = buf[0];
  72.         num_color_trans++;
  73.         }
  74.         strcpy(color_match[num_color_match], buf);
  75.         num_color_match++;
  76.         if (num_color_match>MAX_COLORS) {
  77.         fprintf(stderr,"Overflow on color space: %s (%d)\n",
  78.             filename, image_number);
  79.         for (dummy=0; dummy<num_color_match; dummy++)
  80.             fprintf(stderr,"%s\n", color_match[dummy]);
  81.         exit(16);
  82.         }
  83.     }
  84.     else if (color_match[count][0]!=buf[0]) {
  85.         color_translate[num_color_trans][0] = buf[0];
  86.         color_translate[num_color_trans][1] = color_match[count][0];
  87.         num_color_trans ++;
  88.     }
  89.     dummy++;
  90.     }
  91.  
  92.     /* now we start reading the pixels in */
  93.     dummy = 0;
  94.     while (dummy<24) {
  95.     int i,j;
  96.  
  97.     if (getc(fin)=='/') {
  98.         fgets(buf, 256, fin);
  99.         if (dummy!=0 || strncmp(buf,"* pixels", 8))
  100.             fprintf(stderr,"File %s, Unexpected pixel comment: %s\n", filename, buf);
  101.         continue;
  102.     }
  103.     fgets(buf, 256, fin);
  104.     buf[24] = '\0';
  105.     for (i=0; i<24; i++)
  106.         for (j=0; j<num_color_trans; j++)
  107.         if (buf[i] == color_translate[j][0]) {
  108.             buf[i] = color_translate[j][1];
  109.             break;
  110.         }
  111.     if (image_x==0)
  112.         strcpy(xpm_buf[image_y*24 + dummy], buf);
  113.     else
  114.         strcat(xpm_buf[image_y*24 + dummy], buf);
  115.     dummy++;
  116.     }
  117.     fclose(fin);
  118.     image_x ++;
  119.     if (image_x==XPM_MONTAGE_X) {
  120.     image_x= 0;
  121.     image_y ++;
  122.     }
  123. }
  124.  
  125. int main(int argc, char *argv[])
  126. {
  127.     FILE *fout, *bmaps;
  128.     char buf[256];
  129.     int i,j;
  130.  
  131.     if (argc!=2) {
  132.     fprintf(stderr,"Usage: xpmtopix (filename)\n");
  133.     exit(1);
  134.     }
  135.     if ((bmaps = fopen("bmaps.paths", "r"))==NULL) {
  136.     fprintf(stderr,"Can not open bmaps.paths file\n");
  137.     exit(2);
  138.     }
  139.     sprintf(buf,"%s.%d", argv[1], file_number);
  140.     if ((fout=fopen(buf,"w"))==NULL) {
  141.     fprintf(stderr, "Can not open %s for writing\n", buf);
  142.     fclose(bmaps);
  143.     exit(4);
  144.     }
  145.     while (fgets(buf, 256, bmaps)!=NULL) {
  146.     if (buf[0]!='\\') continue;
  147.     buf[strlen(buf)-1] = '\0';
  148.     strcat(buf,".xpm");
  149.     load_xpm(strchr(buf,'.'));
  150.     image_number ++;
  151.     if (image_number == XPM_MONTAGE_X * XPM_MONTAGE_Y) {
  152.  
  153.         fprintf(fout,"/* XPM */\n");
  154.         fprintf(fout, "static char *file[] = {\n");
  155.         fprintf(fout,"\"%d %d %d 1\",\n", XPM_MONTAGE_X*24, XPM_MONTAGE_Y*24,
  156.         num_color_match);
  157.         for (i=0; i<num_color_match; i++)
  158.         fprintf(fout,"\"%s\",\n", color_match[i]);
  159.         for (i=0; i<XPM_MONTAGE_Y*24-1; i++)
  160.         fprintf(fout,"\"%s\",\n", xpm_buf[i]);
  161.         fprintf(fout,"\"%s\"};\n", xpm_buf[XPM_MONTAGE_Y*24-1]);
  162.         fclose(fout);
  163.         image_number = 0;
  164.         num_color_match = 0;
  165.         image_x = 0;
  166.         image_y = 0;
  167.         file_number++;
  168.         sprintf(buf,"%s.%d", argv[1], file_number);
  169.         if ((fout=fopen(buf,"w"))==NULL) {
  170.         fprintf(stderr, "Can not open %s for writing\n", buf);
  171.         fclose(bmaps);
  172.         exit(4);
  173.         }
  174.     }
  175.     }
  176.     /* Write out the xpm file it was working on.  This will likely not
  177.      * have the full Y dimension */
  178.     fprintf(fout,"/* XPM */\n");
  179.     fprintf(fout, "static char *file[] = {\n");
  180.     fprintf(fout,"\"%d %d %d 1\",\n", XPM_MONTAGE_X*24, 
  181.     (image_x==0? image_y : image_y+1) * 24,
  182.     num_color_match);
  183.     for (i=0; i<num_color_match; i++)
  184.     fprintf(fout,"\"%s\",\n", color_match[i]);
  185.     if (image_x!=0) {
  186.     for (i=image_y*24; i<(image_y+1)*24; i++) {
  187.         for (j=image_x*24; j<XPM_MONTAGE_X*24; j++)
  188.         xpm_buf[i][j]=color_match[0][0];
  189.         xpm_buf[i][j] = '\0';
  190.     }
  191.     }
  192.     for (i=0; i<((image_x==0? image_y : image_y+1) * 24) -1; i++)
  193.     fprintf(fout,"\"%s\",\n", xpm_buf[i]);
  194.     fprintf(fout,"\"%s\"};\n", xpm_buf[i]);
  195.     fclose(fout);
  196.     exit(0);
  197. }
  198.  
  199.  
  200. #if defined(SVR4) && !defined(sun) && !defined(sgi)
  201. int strcasecmp(char *s1, char *s2)
  202. {
  203.   register int c1, c2;
  204.  
  205.   while (*s1 && *s2) {
  206.     c1 = tolower(*s1);
  207.     c2 = tolower(*s2);
  208.     if (c1 != c2)
  209.       return (c1 - c2);
  210.     s1++;
  211.     s2++;
  212.   }
  213.   return (int) (*s1 - *s2);
  214. }
  215. #endif
  216.